﻿/*
* sys_buttoninfo.cs
* 2017-05-17
* 功 能： 按钮数据操作类
* 类 名： sys_buttoninfo
*/
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using FZKJ.DBUtility;//Please add references
namespace FZKJ.DAL
{
	/// <summary>
	/// 数据访问类:sys_buttoninfo
	/// </summary>
	public partial class sys_buttoninfo
	{
		public sys_buttoninfo()
		{}

        /// <summary>
        /// 获取菜单编号
        /// </summary>
        /// <returns></returns>
        public string GetMaxNo()
        {
            string sql = "SELECT RIGHT(1001+ISNULL(RTRIM(RIGHT(MAX(btncode),3)),0),4) from sys_buttoninfo";
            return new SQLHelper().GetSingle(sql).ToString();
        }

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(FZKJ.Model.sys_buttoninfo MODEL)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into sys_buttoninfo(");
			strSql.Append("btncode,btnname,btnicon,btnmethod,btnsort,isenable,createtime,createman,updatetime,updateman,btnclass)");
			strSql.Append(" values (");
            strSql.Append("@btncode,@btnname,@btnicon,@btnmethod,@btnsort,@isenable,@createtime,@createman,@updatetime,@updateman,@btnclass)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@btncode", MODEL.btncode),
					new SqlParameter("@btnname", MODEL.btnname),
					new SqlParameter("@btnicon", MODEL.btnicon),
					new SqlParameter("@btnmethod", MODEL.btnmethod),
					new SqlParameter("@btnsort", MODEL.btnsort),
					new SqlParameter("@isenable", MODEL.isenable),
					new SqlParameter("@createtime", MODEL.createtime),
					new SqlParameter("@createman", MODEL.createman),
					new SqlParameter("@updatetime", MODEL.updatetime),
					new SqlParameter("@updateman", MODEL.updateman),
                    new SqlParameter("@btnclass", MODEL.btnclass)};

			object obj = new SQLHelper().GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(FZKJ.Model.sys_buttoninfo MODEL)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update sys_buttoninfo set ");
			strSql.Append("btnname=@btnname,");
			strSql.Append("btnicon=@btnicon,");
			strSql.Append("btnmethod=@btnmethod,");
			strSql.Append("btnsort=@btnsort,");
			strSql.Append("isenable=@isenable,");
			strSql.Append("createtime=@createtime,");
			strSql.Append("createman=@createman,");
			strSql.Append("updatetime=@updatetime,");
			strSql.Append("updateman=@updateman,");
            strSql.Append("btnclass=@btnclass");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@btnname", MODEL.btnname),
					new SqlParameter("@btnicon", MODEL.btnicon),
					new SqlParameter("@btnmethod", MODEL.btnmethod),
					new SqlParameter("@btnsort", MODEL.btnsort),
					new SqlParameter("@isenable", MODEL.isenable),
					new SqlParameter("@createtime", MODEL.createtime),
					new SqlParameter("@createman", MODEL.createman),
					new SqlParameter("@updatetime", MODEL.updatetime),
					new SqlParameter("@updateman", MODEL.updateman),
					new SqlParameter("@id", MODEL.id),
					new SqlParameter("@btncode", MODEL.btncode),
                    new SqlParameter("@btnclass", MODEL.btnclass)};

			int rows=new SQLHelper().ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from sys_buttoninfo ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows=new SQLHelper().ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string btncode)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from sys_buttoninfo ");
			strSql.Append(" where btncode=@btncode ");
			SqlParameter[] parameters = {
					new SqlParameter("@btncode", SqlDbType.NVarChar,10)			};
			parameters[0].Value = btncode;

			int rows=new SQLHelper().ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from sys_buttoninfo ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=new SQLHelper().ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public FZKJ.Model.sys_buttoninfo GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select  top 1 id,btncode,btnname,btnicon,btnmethod,btnsort,isenable,createtime,createman,updatetime,updateman,btnclass from sys_buttoninfo ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			FZKJ.Model.sys_buttoninfo MODEL=new FZKJ.Model.sys_buttoninfo();
			DataSet ds=new SQLHelper().Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public FZKJ.Model.sys_buttoninfo DataRowToModel(DataRow row)
		{
			FZKJ.Model.sys_buttoninfo MODEL=new FZKJ.Model.sys_buttoninfo();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					MODEL.id=int.Parse(row["id"].ToString());
				}
				if(row["btncode"]!=null)
				{
					MODEL.btncode=row["btncode"].ToString();
				}
				if(row["btnname"]!=null)
				{
					MODEL.btnname=row["btnname"].ToString();
				}
				if(row["btnicon"]!=null)
				{
					MODEL.btnicon=row["btnicon"].ToString();
				}
				if(row["btnmethod"]!=null)
				{
					MODEL.btnmethod=row["btnmethod"].ToString();
				}
				if(row["btnsort"]!=null && row["btnsort"].ToString()!="")
				{
					MODEL.btnsort=int.Parse(row["btnsort"].ToString());
				}
				if(row["isenable"]!=null && row["isenable"].ToString()!="")
				{
					MODEL.isenable=int.Parse(row["isenable"].ToString());
				}
				if(row["createtime"]!=null && row["createtime"].ToString()!="")
				{
					MODEL.createtime=DateTime.Parse(row["createtime"].ToString());
				}
				if(row["createman"]!=null)
				{
					MODEL.createman=row["createman"].ToString();
				}
				if(row["updatetime"]!=null && row["updatetime"].ToString()!="")
				{
					MODEL.updatetime=DateTime.Parse(row["updatetime"].ToString());
				}
				if(row["updateman"]!=null)
				{
					MODEL.updateman=row["updateman"].ToString();
				}
                if (row["btnclass"] != null)
                {
                    MODEL.btnclass = row["btnclass"].ToString();
                }
			}
			return MODEL;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select id,btncode,btnname,btnicon,btnmethod,btnsort,isenable,createtime,createman,updatetime,updateman,btnclass ");
			strSql.Append(" FROM sys_buttoninfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return new SQLHelper().Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
            strSql.Append(" id,btncode,btnname,btnicon,btnmethod,btnsort,isenable,createtime,createman,updatetime,updateman,btnclass ");
			strSql.Append(" FROM sys_buttoninfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return new SQLHelper().Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM sys_buttoninfo ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = new SQLHelper().GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from sys_buttoninfo T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return new SQLHelper().Query(strSql.ToString());
		}
	}
}

